package com.txsj.scan.reader.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.greendao.TagInfoDao;
import com.txsj.scan.R;
import com.txsj.scan.bean.TagInfo;
import com.txsj.scan.db.GreenDaoUtils;
import com.txsj.scan.reader.helper.InventoryBuffer;
import com.txsj.scan.reader.helper.ReaderHelper;

import java.util.ArrayList;
import java.util.List;


public class TagRealList extends LinearLayout {


    private Context mContext;
    private TableRow mTagRealRow;
    private ImageView mTagRealImage;
    private TextView mListTextInfo;
    private TextView mMinRSSIText, mMaxRSSIText;

    private ReaderHelper mReaderHelper;

    private List<InventoryBuffer.InventoryTagMap> data;
    private RealListAdapter mRealListAdapter;
    private ListView mTagRealList;

    private View mTagsRealListScrollView;
    private TextView mUIDText;
    private WindowManager wm;

    private static InventoryBuffer m_curInventoryBuffer;

    public TagRealList(Context context, AttributeSet attrs) {
        super(context, attrs);
        initContext(context);
    }

    public TagRealList(Context context) {
        super(context);
        initContext(context);
    }

    @SuppressWarnings("deprecation")
    private void initContext(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.tag_real_list, this);

        try {
            mReaderHelper = ReaderHelper.getDefaultHelper();
        } catch (Exception e) {
            e.printStackTrace();
        }
        data = new ArrayList();
        m_curInventoryBuffer = mReaderHelper.getCurInventoryBuffer();


        mTagsRealListScrollView = findViewById(R.id.tags_real_list_scroll_view);
        wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        LayoutParams lp = (LayoutParams) mTagsRealListScrollView
                .getLayoutParams();
        lp.height = (int) (wm.getDefaultDisplay().getHeight() / 2.7);
        mTagsRealListScrollView.setLayoutParams(lp);
        mTagsRealListScrollView.invalidate();
        mUIDText = (TextView) findViewById(R.id.uid_text);

        mTagRealRow = (TableRow) findViewById(R.id.table_row_tag_real);
        mTagRealImage = (ImageView) findViewById(R.id.image_prompt);
        mTagRealImage.setImageDrawable(getResources()
                .getDrawable(R.drawable.up));
        mListTextInfo = (TextView) findViewById(R.id.list_text_info);
        mListTextInfo.setText(getResources().getString(R.string.close_tag_list));

        mMinRSSIText = (TextView) findViewById(R.id.min_rssi_text);
        mMaxRSSIText = (TextView) findViewById(R.id.max_rssi_text);

        mTagRealRow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                LayoutParams lp = (LayoutParams) mTagsRealListScrollView
                        .getLayoutParams();

                if (lp.height <= wm.getDefaultDisplay().getHeight() / 2) {
                    // mTagsRealListScrollView.setVisibility(View.VISIBLE);

                    lp.height = (int) (wm.getDefaultDisplay().getHeight() / 1.5);
                    mTagsRealListScrollView.setLayoutParams(lp);
                    mTagsRealListScrollView.invalidate();

                    mTagRealImage.setImageDrawable(getResources().getDrawable(
                            R.drawable.down));
                    mListTextInfo.setText(getResources().getString(
                            R.string.close_tag_list));
                } else {
                    lp.height = (int) (wm.getDefaultDisplay().getHeight() / 2.7);
                    mTagsRealListScrollView.setLayoutParams(lp);
                    mTagsRealListScrollView.invalidate();

                    mTagRealImage.setImageDrawable(getResources().getDrawable(
                            R.drawable.up));
                    mListTextInfo.setText(getResources().getString(
                            R.string.close_tag_list));
                }
            }
        });

        mTagRealList = (ListView) findViewById(R.id.tag_real_list_view);
        mRealListAdapter = new RealListAdapter(mContext, data);
        mTagRealList.setAdapter(mRealListAdapter);

    }

    public final void clearText() {
        mMinRSSIText.setText("0dBm");
        mMaxRSSIText.setText("0dBm");
    }

    public final void refreshText() {
        if (m_curInventoryBuffer.nMinRSSI == 0 && m_curInventoryBuffer.nMaxRSSI == 0) {
            mMinRSSIText.setText("0dBm");
            mMaxRSSIText.setText("0dBm");
        } else {
            mMinRSSIText.setText((m_curInventoryBuffer.nMinRSSI - 129) + "dBm");
            mMaxRSSIText.setText((m_curInventoryBuffer.nMaxRSSI - 129) + "dBm");
        }
    }

    public final void refreshList() {
        data.clear();
        data.addAll(m_curInventoryBuffer.lsTagList);
        mRealListAdapter.notifyDataSetChanged();
        invaildate();
    }

    /**
     * 将读到的数据放入数据库
     */
    public void saveDataToSql() {
        TagInfoDao tagInfoDao = GreenDaoUtils.getSingleTon().getmDaoSession().getTagInfoDao();
        List<TagInfo> list = new ArrayList<>();
        for (int i = 0; i < m_curInventoryBuffer.lsTagList.size(); i++) {
            InventoryBuffer.InventoryTagMap map = m_curInventoryBuffer.lsTagList.get(i);
            TagInfo info = new TagInfo();
            info.setBtAntId(map.btAntId);
            info.setMDate(map.mDate.toString());
            info.setNAnt1(map.nAnt1);
            info.setNAnt2(map.nAnt2);
            info.setNAnt3(map.nAnt3);
            info.setNAnt4(map.nAnt4);
            info.setNReadCount(map.nReadCount);
            info.setStrCRC(map.strCRC);
            info.setStrEPC(map.strEPC);
            info.setStrFreq(map.strFreq);
            info.setStrPC(map.strPC);
            info.setStrRSSI(map.strRSSI);
            tagInfoDao.insertOrReplace(info);
        }
        tagInfoDao.insertOrReplaceInTx(list);
    }

    /**
     * 从数据库获取数据并打印
     */
    public void queryDataFromSql(){
        List<TagInfo> tagInfoList = GreenDaoUtils.getSingleTon().getmDaoSession().loadAll(TagInfo.class);
        for (TagInfo info:tagInfoList){
            String jsonObject = new Gson().toJson(info);
            Log.i("wbj+++++", jsonObject);
        }
    }

    private void invaildate() {
        if (mUIDText != null) {
            mUIDText.setWidth(mRealListAdapter.mWidthest);
            Log.e("change the width", mUIDText.getWidth() + "::::::::::::::");
            mUIDText.invalidate();
        }
    }
}
